package com.project.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {
    
    @Bean
    public CommandLineRunner initDatabase(DataSource dataSource) {
        return args -> {
            ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            populator.addScript(new ClassPathResource("db/schema.sql"));
            populator.addScript(new ClassPathResource("db/data.sql"));
            populator.setSeparator(";");
            populator.setContinueOnError(true);
            
            // 执行数据库初始化脚本
            populator.execute(dataSource);
        };
    }
} 